<?php
namespace Android\Controller;

class HolidayController extends BaseController {


    /**
     * 获取当前教师能够批准的请假条列表
     *
     */
    public function getHolidayList() {

        $teacher_id = I ('request.teacher_id',null);
        $term_id = $this->term['id'];
        if (empty($teacher_id) || empty($term_id))
            $this->out($this->fail,'Empty parameter');

        $page = I ('request.page',1);
        $rows = I ('request.rows',10);
        $limit = ($page-1)*$rows.','.$rows;

        $map['teacher_id'] = $teacher_id;
        $map['term_id'] = $term_id;

        $Holiday = D ('Holiday');

        $holidayArr = $Holiday->getHolidayByWhere($map,$limit);
        if (empty($holidayArr))
            $this->out($this->fail, '没有请假记录');

        $Student = D ('Student');
        $Class = D ('Class');
        foreach ($holidayArr as $k=>$v) {
            $holidayArr[$k]['stime'] = date('Y-m-d H:i',strtotime($v['stime']));
            $holidayArr[$k]['etime'] = date('Y-m-d H:i',strtotime($v['etime']));
            $stu = $Student->getStudentById($v['student_id'],'name,telphone,sex,student_id,class_id,photo');
            $stu['class_name'] = $Class->getFullClassName($stu['class_id']);

            $holidayArr[$k]['student'] = $stu;
        }



        $page_index = $page;
        $page_size = $rows;
        $amount = $Holiday->where($map)->count();
        $page_total = intval(ceil($amount/$page_size));

        $value = array(
            'page_index' => $page_index,
            'page_size' => $page_size,
            'page_total' => $page_total,
            'amount' => $amount,
            'data' => $holidayArr
        );

        $this->out($this->success, 'Success', $value);
    }


    /**
     * 教师处理学生请假信息
     */
    public function handlerHoliday() {

        $data['id'] = I ('request.holiday_id',null);
        $data['status'] = I ('request.status',null);
        $data['response'] = I ('request.response',null);

        if (\Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data) !== '000000')
            $this->out($this->fail,'Empty parameter');

        // 存储处理结果
        $Holiday = D ('Holiday');
        if ($Holiday->handlerHoliday($data) === false) {
            $this->out($this->fail, '处理失败');
        }

        // 向学生推送请假处理结果
        $JPush = new \Api\Libs\JPush();
        $sid = $Holiday->getStudentId($data['id']);
        $JpushMapping = D ('JpushMapping');
        $receiver = $JpushMapping->getReceiverBySid($sid);

        $extras = array('action'=>2);  // 推送参数信息

        if (!empty($receiver)){
            $msg = array(
                'title' => "您的请假已经审批",
                'content' => $this->getStatusInfo($data['status']) . $data['response'],
            );
            $JPush->pushNotification($msg,$receiver,$extras);
        }

        $this->out($this->success, '处理成功', null);
    }


    /**
     * 学生提交请假请求
     *
     */
    public function submitHoliday () {

        $data['term_id'] = $this->term['id'];
        $data['class_id'] = I ('request.class_id',null);
        $data['student_id'] = I ('request.student_id',null);
        $data['stime'] = I ('request.stime',null);
        $data['etime'] = I ('request.etime',null);
        //$data['title'] = I ('request.title',null);
        $data['content'] = I ('request.content',null);
        $data['qsh'] = I ('request.qsh',null);
        $data['person_contact'] = I ('request.person_contact');
        $data['family_contact'] = I ('request.family_contact');

        if (\Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data) !== '000000')
            $this->out($this->fail,'Empty parameter');

        $data['stime'] = date('Y-m-d H:i:s',$data['stime']/1000);
        $data['etime'] = date('Y-m-d H:i:s',$data['etime']/1000);

        $data['teacher_id'] = D ('ClassManager')->getTidByCid($data['class_id']);
        if (empty($data['teacher_id']))
            $this->out($this->fail,'没有找到能够处理该请假信息的教师');

        $data['addtime'] = date('Y-m-d H:i:s');

        $flag = D ('Holiday')->addHoliday($data);
        if ($flag === false)
            $this->out($this->fail, '提交请假条失败');

        // 想相关老师推送请假消息
        $JPush = new \Api\Libs\JPush();
        $JpushMapping2 = D ('JpushMapping2');
        $receiver = $JpushMapping2->getReceiverByTid($data['teacher_id']);

        $extras = array('action' => 3);

        if (!empty($receiver)) {
            $msg = array(
                'title' => '新请假信息',
                'content' => '您有新的请假信息需要处理'
            );
            $JPush->pushNotification($msg,$receiver,$extras);
        }

        $this->out($this->success,'提交成功',null);
    }


    private function getStatusInfo($status) {
        switch ($status) {
            case 1:
                return '[请假已批准]';
            case 2:
                return '[请假未批准]';
            default : return '';
        }
    }



    public function getStudentHoliday () {

        $student_id = I ('request.student_id',null);
        $term_id = $this->term['id'];
        if (empty($student_id) || empty($term_id))
            $this->out($this->fail,'Empty parameter');

//        $page = I ('request.page',1);
//        $rows = I ('request.rows',10);
//        $limit = ($page-1)*$rows.','.$rows;
//
//        $Model = D ('Holiday');
//        $holidayArr = $Model->getStudentHoliday($student_id,$term_id,$limit);
//        if (empty($holidayArr))
//            $this->out($this->fail,'没有任何请假信息');
//
//        foreach ($holidayArr as $k=>$v) {
//            $holidayArr[$k]['stime'] = date('Y-m-d H:i', strtotime($v['stime']));
//            $holidayArr[$k]['etime'] = date('Y-m-d H:i', strtotime($v['etime']));
//        }

        $page = I ('request.page',1);
        $rows = I ('request.rows',10);
        $limit = ($page-1)*$rows.','.$rows;

        $map['student_id'] = $student_id;
        $map['term_id'] = $term_id;

        $Holiday = D ('Holiday');

        $holidayArr = $Holiday->getHolidayByWhere($map,$limit);
        if (empty($holidayArr))
            $this->out($this->fail, '没有请假记录');

        $Student = D ('Student');
        $Class = D ('Class');
        foreach ($holidayArr as $k=>$v) {
            $holidayArr[$k]['stime'] = date('Y-m-d H:i',strtotime($v['stime']));
            $holidayArr[$k]['etime'] = date('Y-m-d H:i',strtotime($v['etime']));
            $stu = $Student->getStudentById($v['student_id'],'name,telphone,sex,student_id,class_id,photo');
            $stu['class_name'] = $Class->getFullClassName($stu['class_id']);

            $holidayArr[$k]['student'] = $stu;
        }


        $page_index = $page;
        $page_size = $rows;
        $amount = $Holiday->where($map)->count();
        $page_total = intval(ceil($amount/$page_size));

        $value = array(
            'page_index' => $page_index,
            'page_size' => $page_size,
            'page_total' => $page_total,
            'amount' => intval($amount),
            'data' => $holidayArr
        );

        $this->out($this->success,'Success',$value);
    }


    public function rescindHoliday () {

        $holiday_id = I ('request.holiday_id',null);
        if (empty($holiday_id))
            $this->out($this->fail,'Empty parameter');

        $Holiday = M ('Holiday');
        $res = $Holiday->find($holiday_id);
        if (empty($res))
            $this->out($this->fail,'请假记录不存在');

        if ($res['status'] != 0)
            $this->out($this->fail,'教师已处理，不能撤销');

        $flag = $Holiday->delete($holiday_id);
        if ($flag === false)
            $this->out($this->fail,'撤销失败');
        $this->out($this->success,'撤销成功');
    }

}